const app = getApp();
Page({
  data: {
    curpage: 1,
    hasmore: true,
    menu_list: [],
    keywords: '',
    searchWords: '',
    showAllmenu: false,
    floorstatus: false,
    topNum: 0
  },
  onLoad: function (options) {
    let class_id = options.class_id || 1;
    this.data.class_id = class_id;
    let self = this;
    let wh;
    wx.getSystemInfo({
      success: res => {
        wh = res.windowHeight;
      }
    })
    const query = wx.createSelectorQuery();
    query.select('.cookbook-con').boundingClientRect();
    query.exec(res => {
      self.setData({
        scrollHeight: wh - res[0].top
      })
    });
    let keyPromise = new Promise((resolve, reject) => {
      wx.getStorage({
        key: 'key',
        success: res => {
          self.data.key = res.data;
          resolve();
        },
        fail: () => {
          self.data.key = '';
          resolve();
        }
      })
    })
    keyPromise.then(() => {
      self.getMenuInfo()
    })
  },
  getMenuInfo: function () {
    let self = this;
    wx.request({
      url: `${app.globalData.ApiUrl}/index.php?act=menu`,
      method: 'GET',
      data: {
        class_id: self.data.class_id,
        keywords: self.data.keywords,
        key: self.data.key,
        curpage: self.data.curpage
      },
      success: res => {
        if (res.data.code == 200) {
          self.setData({
            class_list: res.data.datas.class_list,
            menu_list: [...self.data.menu_list, ...res.data.datas.menu_list]
          })
          self.data.hasmore = res.data.hasmore;
          if (res.data.hasmore) {
            self.data.curpage++
          }
          let curClassIndex = res.data.datas.class_list.findIndex(value => value.checked);
          if (curClassIndex <= 2) {
            this.setData({
              curClassIndex: 0
            })
          } else {
            this.setData({
              curClassIndex: curClassIndex - 2
            })
          }
        }
      }
    })
  },
  changeMenuClass: function (e) {
    let class_id = e.currentTarget.dataset.class_id;
    this.data.class_id = class_id;
    this.data.menu_list = [];
    this.data.curpage = 1;
    this.data.hasmore = true;
    this.setData({
      keywords: '',
      showAllmenu: false
    })
    this.getMenuInfo();
  },
  searchMenu: function (e) {
    this.data.keywords = e.detail.value;
  },
  confirmSearch: function () {
    this.data.class_id = '';
    this.data.menu_list = [];
    this.data.curpage = 1;
    this.data.hasmore = true;
    if (this.data.keywords != '') {
      this.getMenuInfo();
    } else {
      wx.showToast({
        title: '请输入搜索关键字',
        icon: 'none'
      })
    }
  },
  showAllMenuClass: function () {
    this.setData({
      showAllmenu: !this.data.showAllmenu
    })
  },
  viewScroll: function (e) {
    if (e.detail.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  getMoreMenu: function () {
    if (this.data.hasmore) {
      this.getMenuInfo()
    }
  },
  onShow: function () {
    if (this.data.class_id == 0) {
      this.data.menu_list = [];
      this.getMenuInfo();
    }
  },
  toTop: function (e) { // 一键回到顶部
    this.setData({
      topNum: 0
    });
  },
})